﻿// Count_sort.cpp : Сортировка подсчетом
//

#include <iostream>
#include <Windows.h>

void count_sort(int A[], int N)
{
    // Я ОБЯЗАН знать диапазон сортируемых чисел (считаю, что они от 10 до 24)
    int F[50] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    for (int i = 0; i < N; i++) {
        F[A[i]]++;
    }
    int i = 0;
    for (int x = 0; x < 50; x++) {  // перебираю всевозможные значения ключа! (их M штук)
        for (int k = 0; k < F[x]; k++) {
            A[i] = x;
            i++;
        }
    }
}

//распечатка массива
void print_array(int* array, int array_size) {

    std::cout << "Исходный массив: ";
    for (size_t i = 0; i < array_size; i++)
    {
        std::cout << array[i] << ' ';
    }
    std::cout << '\n';

    count_sort(array, array_size);

    std::cout << "Отсортированный массив: ";
    for (size_t i = 0; i < array_size; i++)
    {
        std::cout << array[i] << ' ';
    }
    std::cout << '\n' << '\n';
}

int main()
{
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);

    int A1[]{ 19, 14, 22, 22, 17, 22, 13, 21, 20, 24, 18, 10, 17, 16, 17, 20, 22, 11, 20, 16, 14, 13, 10, 22, 18, 14, 16, 24, 19, 17 };
    int size1 = sizeof(A1) / sizeof(A1[0]);
    print_array(A1, size1);

    int A2[]{ 16, 17, 14, 20, 22, 20, 17, 22, 16, 19, 23, 24, 20, 22, 21, 18, 14, 16, 17, 21, 10, 11, 19, 23, 11, 11, 17, 17, 11, 21, 17, 11, 17, 16, 12, 11, 16, 22, 23, 16, };
    int size2 = sizeof(A2) / sizeof(A2[0]);
    print_array(A2, size2);

    int A3[]{ 21, 15, 19, 18, 23, 12, 18, 18, 19, 23, 12, 20, 15, 22, 21, 18, 19, 20, 12, 16, 20, 14, 17, 13, 10, 23, 19, 14, 10, 22, 19, 12, 24, 23, 22, 15, 13, 22, 18, 18, 11, 23, 24, 17, 10 };
    int size3 = sizeof(A3) / sizeof(A3[0]);
    print_array(A3, size3);
    return 0;
}
